package com.cmc.cloud.cmclink.doc.service.impl;

import java.util.*;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.LocalCostRateErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.LocalCostRateConvert;
import com.cmc.cloud.cmclink.doc.entity.*;
import com.cmc.cloud.cmclink.doc.mapper.LocalCostRateMapper;
import com.cmc.cloud.cmclink.doc.service.LocalCostRateService;
import com.cmc.cloud.cmclink.doc.service.si.CalculateOperateLogService;
import com.cmc.cloud.cmclink.doc.vo.localcostratevo.LocalCostRateCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.localcostratevo.LocalCostRateExportVO;
import com.cmc.cloud.cmclink.doc.vo.localcostratevo.LocalCostRatePageReqVO;
import com.cmc.cloud.cmclink.doc.vo.localcostratevo.LocalCostRateUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;

import cn.hutool.core.bean.BeanUtil;

/**
 * 提单本地费用费率 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class LocalCostRateServiceImpl implements LocalCostRateService {

    @Resource
    private LocalCostRateMapper localCostRateMapper;

    @Resource
    private CalculateOperateLogService calLogService;

    @Override
    public void createLocalCostRate(LocalCostRateCreateReqVO createReqVO) {
        // 插入
        LocalCostRateDO localCostRate = LocalCostRateConvert.INSTANCE.convert(createReqVO);
        localCostRate.setAuditFlag(false);
        localCostRateMapper.insert(localCostRate);
    }

    @Override
    public void updateLocalCostRate(LocalCostRateUpdateReqVO updateReqVO) {
        // 校验存在
        validateLocalCostRateExists(updateReqVO.getId());
        // 更新
        LocalCostRateDO updateObj = LocalCostRateConvert.INSTANCE.convert(updateReqVO);
        localCostRateMapper.updateById(updateObj);
    }

    @Override
    public void deleteLocalCostRate(Long id) {
        // 校验存在
        validateLocalCostRateExists(id);
        // 删除
        localCostRateMapper.deleteById(id);
    }

    private void validateLocalCostRateExists(Long id) {
        if (localCostRateMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(LocalCostRateErrorCodeConstants.LOCAL_COST_RATE_NOT_EXISTS);
        }
    }

    @Override
    public LocalCostRateDO getLocalCostRate(Long id) {
        return localCostRateMapper.selectById(id);
    }

    @Override
    public List<LocalCostRateDO> getLocalCostRateList(Collection<Long> ids) {
        return localCostRateMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<LocalCostRateDO> getLocalCostRatePage(LocalCostRatePageReqVO reqVO) {
        LambdaQueryWrapperX<LocalCostRateDO> queryWrapperX =
            new LambdaQueryWrapperX<LocalCostRateDO>().eqIfPresent(LocalCostRateDO::getPortCode, reqVO.getPortCode())
                .eqIfPresent(LocalCostRateDO::getImExType, reqVO.getImExType());
        return localCostRateMapper.selectPage(reqVO, queryWrapperX);

    }

    @Override
    public void batchApprove(List<Long> ids, boolean b) {
        localCostRateMapper.update(null,
            new LambdaUpdateWrapper<LocalCostRateDO>().set(LocalCostRateDO::getAuditFlag, b)
                .ne(LocalCostRateDO::getAuditFlag, b).in(LocalCostRateDO::getId, ids));
    }

    @Override
    public List<LocalCostRateExportVO> getExportList(String portCode, String imExType) {
        LambdaQueryWrapperX<LocalCostRateDO> queryWrapperX = new LambdaQueryWrapperX<LocalCostRateDO>()
            .eqIfPresent(LocalCostRateDO::getPortCode, portCode).eqIfPresent(LocalCostRateDO::getImExType, imExType);
        List<LocalCostRateDO> localCostRateDos = localCostRateMapper.selectList(queryWrapperX);

        return BeanUtil.copyToList(localCostRateDos, LocalCostRateExportVO.class);
    }


}
